package model

import (
	"cs0512/fmr/pkg/utils"
	"time"
)

type User struct {
	ID        int       `gorm:"primary_key;AUTO_INCREMENT;column:id"`
	CreatedAt time.Time `gorm:"column:created_at"`
	UpdatedAt time.Time `gorm:"column:updated_at"`
	Username  string    `json:"username" gorm:"Column:username;unique;"`
	Password  string    `json:"-" gorm:"Column:password"`
	Salt      string    `json:"-" gorm:"Column:salt"`
	Name      string    `json:"name" gorm:"column:name"`
	Mobile    string    `json:"mobile" gorm:"column:mobile"`
	Area      string    `json:"area" gorm:"column:area"`
	Role      int       `json:"role" gorm:"column:role"`
	OrgId     int       `json:"orgId" gorm:"column:orgId"`
	Status    int       `json:"status" gorm:"column:status"`
}

func (u *User) TableName() string {
	return "users"
}
func (u *User) SetUid(uid int) {
	u.ID = uid
}

func (u *User) CreatePassword(pwd string) {
	u.Salt = utils.RandomString(6)
	u.Password = utils.MD5V([]byte(utils.MD5V([]byte(pwd)) + u.Salt))
}

func (u *User) CheckPassword(pwd string) bool {
	return u.Password == utils.MD5V([]byte(utils.MD5V([]byte(pwd))+u.Salt))
}

type UserList struct {
	ListMeta
	Items []*User `json:"list"`
}

type UserOptions struct {
	ListOptions
	Uid      int
	Username string
	Name     string
	Status   int
	OrgId    int
	Area     string
	Role     int
}
